
<?php
class Gestion_newsletter extends Admin_Controller {

    function Gestion_newsletter() {
        parent::Admin_Controller();
        $this->load->model('Gestion_newsletter_m');
        $this->_data['erreur'] = "";
        $this->_data['listsidebar'] = 'admin/modules/newsletter';
    }
    function index(){
        $this->list_newsletter();
    }
    function list_newsletter() {
        is_admin();
        $this->_data['page_title'] = "la liste des newsletter";
        $per_page = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($per_page == "") {
            $per_page = 10;
        }
        if ($offset == "") {
            $offset = 0;
        }
        $config['base_url'] = base_url() . 'index.php/gestion_newsletter/list_newsletter/' . $per_page . '/';
        $config['per_page'] = '10';
        $config['uri_segment'] = 4;

        $config['full_tag_open'] = "<div id='containpage'>";
        $config['full_tag_close'] = '</div>';
        $config['cur_tag_open'] = '<span id="pageactive"> ';
        $config['cur_tag_close'] = ' </span>';
        $config['next_link'] = '&gt;&gt;';
        $config['prev_link'] = '&lt;&lt;';

        $config['total_rows'] = $this->db->count_all('newsletter');
        if ($config['total_rows'] == $offset)
            $offset = $offset - $per_page;
        $this->_data['perpage'] = $config['per_page'];
        $this->_data['offset'] = $offset;
        $this->_data['query'] = $this->Gestion_newsletter_m->get_all_newsletter($config['per_page'], $offset);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->db->count_all('newsletter');
        $this->admin_view('admin/newsletter/newsletter');
    }
     function detail_newsletter($id, $perpage, $offset) {
        is_admin();
                     $config = array(
            array('field' => 'titre',
                'rules' => 'required'
            ),
            array('field' => 'contenu',
                'rules' => 'required'
            )
        );
         $this->form_validation->set_rules($config);
         if($this->input->post('submit')){
                if($this->form_validation->run()){
                   $ad=$this->input->post('ad');
                   $nonad=$this->input->post('nonad');

              if($ad==""&&$nonad==""){
                $this->_data['erreur'] = "This field is required!";
              }else{
                $titre=$this->input->post('titre');
                $contenu=$this->input->post('contenu');

             if($nonad!="")
             {$query=$this->Gestion_newsletter_m->get_destination('email');
               foreach ($query as $aRow):
                mail($aRow['email'], $titre, $contenu, "From: contact@gwebcreation.com" );
             endforeach;
             }
             if($ad!="")
             {$query=$this->Gestion_newsletter_m->get_destination('inscription');
               foreach ($query as $aRow):
            
             mail($aRow['email'], $titre, $contenu, "From: contact@gwebcreation.com" );
             endforeach;
             }
             $dat=date('Y-m-d');
             $data = array(
                        'titre' => $titre,
                         'dat' => $dat,
                        'contenu' => $contenu
                       );
             $query=$this->Gestion_newsletter_m->update_newsletter($id,$data);
              $this->_data['annonce'] = "Newsletter a &eacute;t&eacute; envoy&eacute; aux clients !";
              }

         }}

        $this->_data['page_title'] = "Newsletter";
        $this->_data['query'] = $this->Gestion_newsletter_m->get_newsletter($id);
         $this->_data['perpage'] = $perpage;
        $this->_data['offset'] = $offset;
        $this->admin_view('admin/newsletter/detail_newsletter');
    }
      function add_newsletter() {
        is_admin();
                     $config = array(
            array('field' => 'titre',
                'rules' => 'required'
            ),
            array('field' => 'contenu',
                'rules' => 'required'
            )
        );
         $this->form_validation->set_rules($config);
         if($this->input->post('submit')){
                if($this->form_validation->run()){
                  $ad=$this->input->post('ad');
                   $nonad=$this->input->post('nonad');

              if($ad==""&&$nonad==""){
                $this->_data['erreur'] = "This field is required!";
              }else{
                $titre=$this->input->post('titre');
                $contenu=$this->input->post('contenu');

             if($nonad!="")
             {$query=$this->Gestion_newsletter_m->get_destination('email');
               foreach ($query as $aRow):
               mail($aRow['email'], $titre, $contenu, "From: contact@gwebcreation.com" );
             endforeach;
             }
             if($ad!="")
             {$query=$this->Gestion_newsletter_m->get_destination('inscription');
               foreach ($query as $aRow):
                mail($aRow['email'], $titre, $contenu, "From: contact@gwebcreation.com" );
             endforeach;
             }

             $dat=date('Y-m-d');
             $data = array(
                        'titre' => $titre,
                         'dat' => $dat,
                        'contenu' => $contenu
                       );
             $query=$this->Gestion_newsletter_m->add_newsletter($data);
              $this->_data['annonce'] = "Newsletter a &eacute;t&eacute; envoy&eacute; aux clients !";
              }

         }}

        $this->_data['page_title'] = "Newsletter";
        $this->admin_view('admin/newsletter/add_newsletter');
    }
     function delete_newsletter($id, $perpage, $offset) {
        is_admin();
        $query = $this->Gestion_newsletter_m->delete_newsletter($id);
        if ($query == 1)
            redirect(site_url('gestion_newsletter/list_newsletter' . '/' . $perpage . '/' . $offset));
    }
  function email(){
      is_admin();
         $this->_data['page_title'] = "Email des non adherent";
         $per_page = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($per_page == "") {
            $per_page = 10;
        }
        if ($offset == "") {
            $offset = 0;
        }
        $config['base_url'] = base_url() . 'index.php/gestion_newsletter/email/' . $per_page . '/';
        $config['per_page'] = '10';
        $config['uri_segment'] = 4;

        $config['full_tag_open'] = "<div id='containpage'>";
        $config['full_tag_close'] = '</div>';
        $config['cur_tag_open'] = '<span id="pageactive"> ';
        $config['cur_tag_close'] = ' </span>';
        $config['next_link'] = '&gt;&gt;';
        $config['prev_link'] = '&lt;&lt;';

        $config['total_rows'] = $this->db->count_all('email');
        if ($config['total_rows'] == $offset)
            $offset = $offset - $per_page;
        $this->_data['perpage'] = $config['per_page'];
        $this->_data['offset'] = $offset;
        $this->_data['query'] = $this->Gestion_newsletter_m->get_all_email($config['per_page'], $offset);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->db->count_all('newsletter');
        $this->admin_view('admin/newsletter/email');

    }
    function add_email(){
           is_admin();
             $this->_data['page_title'] = "Ajouter email";
                     $config = array(
            array('field' => 'email',
                'rules' => 'required|valid_email'
            )
        );
         $this->form_validation->set_rules($config);
         if($this->input->post('submit')){
             if($this->form_validation->run()){
                 $email=$this->input->post('email');
                $data = array( 'email' => $email);
             $query=$this->Gestion_newsletter_m->add_email($data);
             redirect('gestion_newsletter/email');
             }
         }
        $this->admin_view('admin/newsletter/add_email');
         }
        function delete_email($id,$perpage,$offset){
            $query=$this->Gestion_newsletter_m->delete_email($id);
             redirect('gestion_newsletter/email/'.$perpage.'/'.$offset);
        }

}
?>
